package com.aos.erp.schools.service.mq;

import com.aos.erp.schools.model.student.StudentResultInfo;
import com.aos.erp.schools.model.student.dto.IntranetResultDto;
import com.aos.erp.schools.service.student.StudentResultInfoService;
import com.aos.erp.schools.service.student.StudentService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 得到内网返回的
 */
@Slf4j
@Component
public class StudentListenerReceiver {


    @Resource
    private StudentService studentService ;
    @Resource
    private StudentResultInfoService studentResultInfoService;
    //内网  返回是否 成功  还没有审核 一般都是成功
    @RabbitListener(queues = QueueConstants.toSchoolStudentSubmitted)
    public void hasSchoolSubmitted(@Payload List<String> idnumList, Channel channel, Message message) throws IOException {
        studentService.updateNetStatusSuccess(idnumList);
        log.info("hasSchoolSubmitted:" + idnumList);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    // 122 审核成功
    @RabbitListener(queues = QueueConstants.toSchoolStudentSuccess)
    public void toSchoolStudentSuccess(@Payload List<String> idnumList, Channel channel, Message message) throws IOException {
        studentService.signUpOkStatus(idnumList);
        log.info("toSchoolStudentSuccess:" + idnumList);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    // 122 审核失败
    @RabbitListener(queues = QueueConstants.toSchoolStudentFailed)
    public void toSchoolStudentFailed(@Payload List<IntranetResultDto> dto, Channel channel, Message message) throws Exception {
        log.info("toSchoolStudentFailed:" + dto);
        for(IntranetResultDto resultDto : dto)
        {
            studentService.signUpFaildStatus(resultDto.getIdNo());
            StudentResultInfo studentResultInfo=new StudentResultInfo();
            BeanUtils.copyProperties(resultDto,studentResultInfo);
            studentResultInfo.setCreateTime(new Date());
            studentResultInfoService.saveBean(studentResultInfo);
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}